package com.kl.forum.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kl.forum.dao.ArticleCommentLightMapper;
import com.kl.forum.dao.ArticleCommentMapper;
import com.kl.forum.dao.ArticleCommentReplyMapper;
import com.kl.forum.entity.ArticleComment;
import com.kl.forum.entity.ArticleCommentLight;
import com.kl.forum.entity.ArticleCommentReply;
import com.kl.forum.enums.SortCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleCommentService {
    @Autowired
    private ArticleCommentMapper articleCommentMapper;
    @Autowired
    private ArticleCommentReplyMapper articleCommentReplyMapper;
    @Autowired
    private ArticleCommentLightMapper articleCommentLightMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public PageInfo findArticleCommentBySort(Integer userId,Integer articleId,Integer pageNum ,Integer pageSize,String sort){
        PageHelper.startPage(pageNum, pageSize,true);
        Map map=new HashMap();
        map.put("articleId",articleId);
        map.put("userId",userId);
        map.put("sort",sort);
        List<ArticleComment> list=articleCommentMapper.findArticleCommentBySort(map);
        for (int i=0;i<list.size();i++){
//            if (!stringRedisTemplate.opsForHash().hasKey("articleCommentLightNum",list.get(i).getId().toString())){
//                stringRedisTemplate.opsForHash().put("articleCommentLightNum",list.get(i).getId().toString(),"0");
//            }
//            Object articleCommentLightNum=  stringRedisTemplate.opsForHash().get("articleCommentLightNum",list.get(i).getId().toString());
//            list.get(i).setLightNum(Integer.valueOf(articleCommentLightNum.toString()));
            if (userId!=null){
                ArticleCommentLight articleCommentLight= articleCommentLightMapper.findArticleCommentLightOne(list.get(i).getId(),userId);
                if(articleCommentLight!=null){
                    list.get(i).setLightStatus(articleCommentLight.getStatus());
                }else {
                    list.get(i).setLightStatus(0);
                }
            }
            map.put("commentId",list.get(i).getId());
            PageHelper.startPage(1, 5,true);
            List list1=articleCommentReplyMapper.findArticleCommentReply(map);
            PageHelper.clearPage();
            PageInfo<Map> pageInfo1 = new PageInfo<>(list1);
            list.get(i).setPageInfoReply(pageInfo1);
        }
        PageInfo<ArticleComment> pageInfo = new PageInfo<>(list);
        PageHelper.clearPage();
        if(SortCriteria.DESCLIGHT.getCode().equals(sort)){
            if (pageInfo.getTotal()<=5){
                return null;
            }
        }
        return pageInfo;
    }

    public PageInfo findArticleCommentByLightNum(Integer userId,Integer articleId,Integer pageNum ,Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("articleId",articleId);
        map.put("userId",userId);
        List<Map> list=articleCommentMapper.findArticleCommentByLightNum(map);
        for(Map item:list){
            map.put("commentId",Integer.valueOf(item.get(item.get("id")).toString()));
            List list1=articleCommentReplyMapper.findArticleCommentReply(map);
            PageInfo<Map> pageInfo = new PageInfo<>(list1);
            item.put("PageInfoReply",pageInfo);
        }
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }



    public int addArticleComment(ArticleComment articleComment){
     return    articleCommentMapper.insertSelective(articleComment);
    }
    public  int update(ArticleComment  articleComment){
        return  articleCommentMapper.updateByPrimaryKeySelective(articleComment);
    }
    public  ArticleComment findArticleCommentById(int articleCommentId){
        return  articleCommentMapper.selectByPrimaryKey(articleCommentId);

    }
    public int delArticleComment(Integer articleCommentId) {
        return  articleCommentMapper.deleteByPrimaryKey(articleCommentId);

    }
}
